package com.yunao.commons.metasearch.entity;

import com.yunao.commons.metasearch.ms.Compare;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;
/**
 * 抓取结果集合
 *
 * @author glt
 *
 */
public class CatchResults  implements List<CatchResult> {

    private List<CatchResult> results;
    private int total = 0;
    private Map<String, String> map;

    public CatchResults() {
        results = new ArrayList<CatchResult>();
        map = new HashMap<String, String>();
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public boolean add(CatchResult e) {
        return results.add(e);
    }

    public void add(int index, CatchResult element) {
        results.add(index, element);
    }

    public boolean addAll(Collection<? extends CatchResult> c) {
        return results.addAll(c);
    }

    public boolean addAll(int index, Collection<? extends CatchResult> c) {
        return results.addAll(index, c);
    }

    public void clear() {
        results.clear();
    }

    public boolean contains(Object o) {
        return results.contains(o);
    }

    public boolean containsAll(Collection<?> c) {
        return results.containsAll(c);
    }

    public CatchResult get(int index) {
        return results.get(index);
    }

    public int indexOf(Object o) {
        return results.indexOf(o);
    }

    public boolean isEmpty() {
        return results.isEmpty();
    }

    public Iterator<CatchResult> iterator() {
        return results.iterator();
    }

    public int lastIndexOf(Object o) {
        return results.lastIndexOf(o);
    }

    public ListIterator<CatchResult> listIterator() {
        return results.listIterator();
    }

    public ListIterator<CatchResult> listIterator(int index) {
        return results.listIterator(index);
    }

    public boolean remove(Object o) {
        return results.remove(o);
    }

    public CatchResult remove(int index) {
        return results.remove(index);
    }

    public boolean removeAll(Collection<?> c) {
        return results.removeAll(c);
    }

    public boolean retainAll(Collection<?> c) {
        return results.retainAll(c);
    }

    public CatchResult set(int index, CatchResult element) {
        return results.set(index, element);
    }

    public int size() {
        return results.size();
    }

    public List<CatchResult> subList(int fromIndex, int toIndex) {
        return results.subList(fromIndex, toIndex);
    }

    public Object[] toArray() {
        return results.toArray();
    }

    @SuppressWarnings("hiding")
    public <T> T[] toArray(T[] a) {
        return results.toArray(a);
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Override
    public String toString() {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("total", total);
        jsonObj.put("size", results.size());
        JSONArray jsonArr = new JSONArray();
        for (CatchResult result : results) {
            jsonArr.add(result);
        }
        jsonObj.put("result", jsonArr);
        return jsonObj.toString();
    }

    @SuppressWarnings("unchecked")
    public void sort() {
        //	System.out.println("正在排序");
        Compare articleComparator = new Compare();
        Collections.sort(results, articleComparator);
    }

    public List<String> getpiclists() {
        List <String> piclists=new ArrayList<String>();
        for(int i=0;i<results.size();i++)
        {
            if(results.get(i).getImgs()!=null&&results.get(i).getImgs().size()>0)
            {//System.out.println(results.get(i).getImgs().size());

                piclists.addAll(results.get(i).getImgs());

            }
            else if(results.get(i).getImg()!=null&&!results.get(i).getImg().equals(""))
                piclists.add(results.get(i).getImg());

            //if(results.get(i).getVdpic()!=null)
            //	piclists.add(results.get(i).getVdpic());


            if(results.get(i).getRer()!=null&&results.get(i).getRer().getImgs()!=null&&results.get(i).getRer().getImgs().size()>0)
            {//System.out.println(results.get(i).getImgs().size());

                piclists.addAll(results.get(i).getRer().getImgs());

            }
            else if(results.get(i).getRer()!=null&&results.get(i).getRer().getImg()!=null&&!results.get(i).getRer().getImg().equals(""))
                piclists.add(results.get(i).getRer().getImg());

            //if(results.get(i).getRer()!=null&&results.get(i).getRer().getVdpic()!=null)
            //	piclists.add(results.get(i).getRer().getVdpic());

        }
        return piclists;
    }

    public String getzhibo() {
        String zhibo="";
        for(int i=0;i<results.size();i++)
        {
            String summary=results.get(i).getSummary().replace("\"", "'").replaceAll("<.*?>","");

            zhibo+="   "+results.get(i).getPubdate()+"("+results.get(i).getAuthor()+"):"+"<a onclick=\"javascript:gotodetailhref('"+results.get(i).getHref()+"')\" src="+results.get(i).getHref()+" >"+summary+"</a>";

        }
        return zhibo;



    }

}
